package com.BeyondCreative.bdolist;

import java.util.Calendar;
import java.util.Locale;

import com.adapter.ColorSelectAdapter;
import com.adapter.DoListAdapter;
import com.dblibrary.DaoClass;
import com.dblibrary.DoListOpenHelper.DoListColumns;

import android.annotation.TargetApi;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class TitleFragment extends Fragment implements OnClickListener,
		OnItemLongClickListener, OnTouchListener, OnItemSelectedListener {

	DoListAdapter adapterFirst;
	DoListAdapter adapterSecond;
	DoListAdapter adapterThird;
	ListView listViewFirst;
	ListView listViewSecond;
	ListView listViewThird;
	String inputText;
	int dayFlag;
	Spinner colorSpinner;
	ColorSelectAdapter spinnerAdapter;
	EditText input_editor;
	DaoClass dbDao;
	TextView dateTv;
	private static int colorFlag = 2;
	SoundPool pool;
	private int papersound;

	public TitleFragment() {
		super();
	}

	public static TitleFragment newInstance(int nowPage) {
		TitleFragment fragment = new TitleFragment();
		Bundle args = new Bundle();
		args.putInt("dayFlag", nowPage);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if ((savedInstanceState != null)) {
		}
		dayFlag = getArguments().getInt("dayFlag");
		dbDao = DaoClass.getInstance(getActivity(), dayFlag);
		adapterFirst = new DoListAdapter(getActivity().getApplicationContext(),
				dayFlag, 0);
		adapterSecond = new DoListAdapter(
				getActivity().getApplicationContext(), dayFlag, 1);
		adapterThird = new DoListAdapter(getActivity().getApplicationContext(),
				dayFlag, 2);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.dolistmain, container, false);
		listViewFirst = (ListView) layout.findViewById(R.id.listView1);
		listViewSecond = (ListView) layout.findViewById(R.id.listView2);
		listViewThird = (ListView) layout.findViewById(R.id.listView3);
		colorSpinner = (Spinner) layout
				.findViewById(R.id.spinner_colorselector);
		input_editor = (EditText) layout.findViewById(R.id.editText1);
		Button add_btn = (Button) layout.findViewById(R.id.button1);
		dateTv = (TextView) layout.findViewById(R.id.dateTextviewId);

		// sound set
		pool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		papersound = pool.load(getActivity(), R.raw.sound_2, 1);

		setDateTv(dayFlag);
		spinnerAdapter = new ColorSelectAdapter(getActivity());

		add_btn.setOnClickListener(this);
		colorSpinner.setAdapter(spinnerAdapter);
		listViewFirst.setAdapter(adapterFirst);
		listViewSecond.setAdapter(adapterSecond);
		listViewThird.setAdapter(adapterThird);
		colorSpinner.setOnItemSelectedListener(this);

		listViewFirst.setOnItemLongClickListener(this);
		listViewSecond.setOnItemLongClickListener(this);
		listViewThird.setOnItemLongClickListener(this);
		listViewFirst.setOnTouchListener(this);
		listViewSecond.setOnTouchListener(this);
		listViewThird.setOnTouchListener(this);

		return layout;
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	private void setDateTv(int dayFlag2) {
		final Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		c.add(Calendar.DAY_OF_WEEK, -1);
		String weekYday = c.getDisplayName(Calendar.DAY_OF_WEEK,
				Calendar.SHORT, Locale.getDefault());
		c.add(Calendar.DAY_OF_WEEK, 1);
		String weekToday = c.getDisplayName(Calendar.DAY_OF_WEEK,
				Calendar.SHORT, Locale.getDefault());
		c.add(Calendar.DAY_OF_WEEK, 1);
		String weekTomor = c.getDisplayName(Calendar.DAY_OF_WEEK,
				Calendar.SHORT, Locale.getDefault());
		if (dayFlag2 == 0) {
			dateTv.setText(" " + year + "." + (month + 1) + "." + (day - 1)
					+ "." + weekYday + "   ");
		} else if (dayFlag2 == 1) {
			dateTv.setText(" " + year + "." + (month + 1) + "." + day + "."
					+ weekToday + "   ");
		} else if (dayFlag2 == 2) {
			dateTv.setText(" " + year + "." + (month + 1) + "." + (day + 1)
					+ "." + weekTomor + "   ");
		} else {
			dateTv.setText("");
		}

	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onClick(View v) {

		String inputString = input_editor.getText().toString();
		dbDao.insert(inputString, colorFlag);
		if (colorFlag == DoListColumns.CN_IMPORTANCE_red) {
			adapterFirst.initContentList();
			adapterFirst.notifyDataSetChanged();
		} else if (colorFlag == DoListColumns.CN_IMPORTANCE_cryon) {
			adapterSecond.initContentList();
			adapterSecond.notifyDataSetChanged();
		} else {
			adapterThird.initContentList();
			adapterThird.notifyDataSetChanged();
		}
		input_editor.setText("");
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		int dbid = (Integer) arg1.getTag();
		dbDao.delete(dbid);
		adapterFirst.initContentList();
		adapterFirst.notifyDataSetChanged();
		adapterSecond.initContentList();
		adapterSecond.notifyDataSetChanged();
		adapterThird.initContentList();
		adapterThird.notifyDataSetChanged();
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		pool.play(papersound, 1.0f, 1.0f, 0, 0, 1);
		/*
		 * if (v.isSoundEffectsEnabled()) {
		 * v.playSoundEffect(SoundEffectConstants.CLICK); }
		 */
		if (v.getId() == listViewFirst.getId()) {
			colorFlag = 0;
			colorSpinner.setSelection(0);
		} else if (v.getId() == listViewSecond.getId()) {
			colorFlag = 1;
			colorSpinner.setSelection(1);
		} else if(v.getId() == listViewThird.getId()) {
			colorFlag = 2;
			colorSpinner.setSelection(2);
		}else
		{
			
		}
		return false;
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
			long arg3) {
		colorFlag = pos;
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}
}
